<template>
    <div class="login_container">
        <!-- 背景图 -->
        <div class="login_pic">
            <div class="avatar_box">
                <img src="../assets/exercise.jpg" class="pic"/>
            </div> 
        </div>
        <!-- 登录框 -->
        <div class="login_box">

            <el-form ref="loginForm" :rules="rules" :model="login" label-width="80px">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="login.username" prefix-icon="iconfont icon-yonghuming"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="login.password" prefix-icon="iconfont icon-mima"></el-input>
                    </el-form-item>
                    <el-form-item class="btns">
                        <el-button type="info" @click="loginForm()">提交</el-button>
                        <el-button @click="resetForm()">重置</el-button>
                    </el-form-item>
                    
            </el-form>
        </div>
    </div>
</template>
 
<script>
export default {
    name: 'Login',
    data(){
        return {
            //表单数据
            login:{
                username:"admin003",
                password:"q123456"
            },
            //检验表单数据
            rules:{
                username:[
                    { required: true, message: '用户名为必填项', trigger: 'blur' },
                    { min: 5, max: 12, message: '长度在 5 到 12个字符', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '密码为必填项', trigger: 'blur' },
                    { min: 5, max: 10, message: '长度在 5 到 10个字符', trigger: 'blur' },
                    { pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{5,10}$/, message: '必须同时包含字母和数字',trigger: 'blur'}
                ]
            },
            
        
        }
    },
    methods:{
        resetForm(){
            this.$refs.loginForm.resetFields();        
        },
        loginForm(){
            this.$refs.loginForm.validate(valid=>{
                if(!valid) return;

                this.$http.post("login",this.login).then(
                    response=>{
                        if(response.data.flag== 1){
                            this.$message.success(response.data.msg)
                            console.log(response.data.data.user.username)
                            window.sessionStorage.setItem("user",response.data.data.user.username)
                            this.$router.push({
                                name:'home'
                            })
                            
                        }else{
                            this.$message.error(response.data.msg)
                        }
                       
                }
                )
            
            })
        }
    }

}
</script>

<style lang="less" scoped>

.login_box {
  width: 400px;
  height: 300px;
  
  border-radius: 3px;// 圆角
  position: absolute;// 绝对定位
  top: 20%;
}
.btns {
    position: absolute;
    left: 17%;

}
.pic {
    width: 100%;
    height: 100%;
}
</style>
